package test29.StreamAPI;

import org.junit.Test;
import test29.Lambda.Employee;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

//测试Stream的终止操作
public class StreamAPITest2 {
    //1-匹配与查找
    @Test
    public void test1() {
        List<Employee> employees = EmployeeData.getEmployees();
        //allMatch(Predicate p),检查是否匹配所有元素（判断是否都为true）
        //练习：是否所有的员工都大于18
        boolean allMatch = employees.stream().allMatch(e -> e.getAge() > 18);
        System.out.println(allMatch); //false(都大于18才为true)

        // anyMatch(Predicate p)，存在即可
        //练习：是否存在员工的工资大于10000
        boolean anyMatch = employees.stream().anyMatch(e -> e.getSalary() > 10000);
        System.out.println(anyMatch);

        //noneMatch(Predicate p)
        //练习：是否存在员工姓”雷“
        boolean noneMatch = employees.stream().noneMatch(e -> e.getName().startsWith("雷"));
        System.out.println(noneMatch); //没有返回true，有就返回false

        //findFirst()--返回第一个元素
        Optional<Employee> first = employees.stream().findFirst();
        System.out.println(first);

        //findAny()--返回当前流中的任意元素
        Optional<Employee> any = employees.parallelStream().findAny();
        System.out.println(any);
    }

    @Test
    public void test2(){
        List<Employee> employees = EmployeeData.getEmployees();
        //count()
        long count = employees.stream().filter(e -> e.getSalary() > 5000).count();
        System.out.println(count);
        //max(Comparator c)
        //返回最高的工资
        Stream<Double> salaryStream = employees.stream().map(e -> e.getSalary());
        Optional<Double> max = salaryStream.max(Double::compare);
        System.out.println(max);
        //min(Comparator c)
        //返回最低的工资的员工
        Optional<Employee> min = employees.stream().min((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()));
        System.out.println(min);
        // forEach(Consumer c)--内部迭代
        employees.stream().forEach(System.out::println);
        //使用集合的遍历操作
        employees.forEach(System.out::println);
    }

    //2-归约
    @Test
    public void test3(){
        //reduce(T iden, BinaryOperator b)
        //练习：计算1-10的自然数的和
        List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8);
        Integer reduce = list.stream().reduce(0, Integer::sum);//第一个参数就是初始值，0
        System.out.println(reduce);

        //reduce(BinaryOperator b)
        //练习：计算公司所有员工工资的总和
        List<Employee> employees = EmployeeData.getEmployees();
        Stream<Double> salaryStream = employees.stream().map(Employee::getSalary);
//        Optional<Double> sumMoney = salaryStream.reduce(Double::sum);
        Optional<Double> sumMoney = salaryStream.reduce((d1, d2) -> d1 + d2);
        System.out.println(sumMoney);

    }

    //3-收集
    @Test
    public void test4(){
        //collect(Collector c)
        //练习：查找工资大于6000的员工，结果返回为一个list或set
        List<Employee> employees = EmployeeData.getEmployees();
        List<Employee> collect = employees.stream().filter(e -> e.getSalary() > 6000).collect(Collectors.toList());
        collect.forEach(System.out::println);

        Set<Employee> collect1 = employees.stream().filter(e -> e.getSalary() > 6000).collect(Collectors.toSet());
        collect1.forEach(System.out::println);

    }
}
